function IncLetterSend() 
{
	this.TAG = " < IncLetterSend > ";
	console.log(this.TAG);
	thisIncLetterSend = this;
	
	this.letterList = null;
	this.letterTotalList = null;
	
	this.pageControl = null;
	
	this.HTMLDom = null;
	
}

IncLetterSend.prototype = {
		getMySentMessageList: function() {
			console.log(this.TAG + "getMySentMessageList()");
			
			this.letterList = null;
			this.letterList = new Array();

			var mj = new MJson();
			var cm = new ConnectionManager();

			mj.putClient(MJson.CLIENT.USERID, document.globalObj.LOGIN_DATA.userId);
			
			mj.putPub(MJson.PUB.SID, "US001");
			mj.putPub(MJson.PUB.SERVICENAME, "getMySentMessageList");
			mj.putPub(MJson.PUB.LISTCOUNT, this.pageControl.listCount);
			mj.putPub(MJson.PUB.CURRPAGENO, this.pageControl.currentPage);

			cm.requestGet(mj.getJsonString(), function(jsonData) {
				//console.log(JSON.stringify(jsonData));
				if (mj.checkServiceResult(jsonData)) {
					if(jsonData.msp.contents)
					{
						if( jsonData.msp.contents.items ) 
						{
							if(jsonData.msp.contents.items.length) {
								for(var i=0; i<jsonData.msp.contents.items.length;i++) 
								{
									thisIncLetterSend.letterList.push(jsonData.msp.contents.items[i]);
								}
							}
							else 
							{
								thisIncLetterSend.letterList.push(jsonData.msp.contents.items);
								
							}
							thisIncLetterSend.viewList(jsonData.msp.contents["@totalcount"]);
						}
					}
				}
			});
			
		},
		onPageShow: function() 
		{
			console.log(this.TAG + "onPageShow()");
			
			this.letterTotalList = new Array();
			this.pageControl = new PageControl();
			this.pageControl.setListCount(3);
			
			this.HTMLDom = {
		            "listview" : $("#letter_send_listview"), // 신청자 리스트뷰
		            "item" : $("#letter_send_listview").find('li:first').clone(true)
		            };
			
			this.getMySentMessageList();

			$(".count_01").html(document.globalObj.NOTIFICATION_COUNT_DATA.booking);
			$(".count_02").html(document.globalObj.NOTIFICATION_COUNT_DATA.notification);
			$(".count_03").html(document.globalObj.NOTIFICATION_COUNT_DATA.letter);
		},
		viewList: function($totalCount) 
		{
			console.log(this.TAG + "viewList()");

			for(var i=0; i<this.letterList.length; i++) {
				this.letterTotalList.push(this.letterList[i]);
			}
			
			var listview = this.HTMLDom.listview;
			listview.empty();
			var item = this.HTMLDom.item;
			
			var list = this.letterTotalList;
			
			//console.log(list);
			
			var util = new Util();
					
			for(var i=0;i<list.length; i++) 
			{
				var data = list[i];
				
				var temp = item.clone(true);
				
				listview.append(temp);
				temp.find("input, label").remove();
				temp.find("a").before("<label><input type='checkbox' data-mini='true' id=lettersend_"+i+" /></label>");
				listview.find("input").eq(i).checkboxradio();
				temp.attr("id", i);
				temp.find("a").attr("href", "#").attr("id", i);
				var senderType = data.messageIn.senderType == "P"?"mem ui-link-inherit":"cl ui-link-inherit";
				temp.find("a:eq(0)").attr("class", senderType);
				temp.find("span[class='state']").html(data.messageIn.senderType === "P"?"회원":"동호회");
				temp.find("span[class='tit']").html(data.messageIn.recUserInfo.nickname);
				temp.find("span[class='sub']").html(data.messageBody);
				temp.find("time").html(util.fTime(data.sendTime, ":"));		
			}

			listview.listview("refresh");

			var addBtn = $("#letter_send_add_btn");
			addBtn.show();

			if(listview.find("li").length >= $totalCount) {
				addBtn.hide();
			}
			
			listview.find("a").on("click", function($event){
				console.log(thisIncLetterSend.TAG + "아이템 클릭!!!");
				var id = $(this).attr("id");
				var data = thisIncLetterSend.letterTotalList[id];
				
				document.globalObj.LETTER_DATA.q_seq = data.messageOutSeq;
				document.globalObj.LETTER_DATA.q_type = "OUT";
				document.globalObj.LETTER_DATA.q_ynSaved = "N";

				$.mobile.changePage("letter_view.html");

				/*
			        this.LETTER_DATA = {//쪽지 view 에 필요 한 데이터
			            q_seq : "80"//해당 시컨스
			            ,
			            q_type : 'OUT'//쪽지 타입 수신 :  IN,    발신  : OUT
			            ,
			            q_ynSaved : 'Y' // 보관여부 : Y or N
			        };
				 */
				
			});

			addBtn.unbind("click");
			addBtn.on("click", function($event){
				console.log("더보기!!");
				thisIncLetterSend.pageControl.addView();
				thisIncLetterSend.getMySentMessageList();
			});
			
			var tSelector = $("#send_t_checkbox");
			tSelector.bind( "change", function(event, ui) {
				//console.log(thisIncLetterSend.TAG + "전체선택:"+$(this).is(":checked"));
				var isChecked = $(this).is(":checked");

				listview.find("input[type='checkbox']").attr("checked", isChecked );
				listview.find("input[type='checkbox']").checkboxradio("refresh");
				
				isChecked =! isChecked;
				
			});
			
			var saveBtn = $("#letter_send_save_btn");
			saveBtn.unbind("click");
			saveBtn.on("click",function($event){
				console.log(thisIncLetterSend.TAG + "보관버튼!");

				thisIncLetterSend.saveMessage();
			});
			
			var delBtn = $("#letter_send_delete_btn");
			delBtn.unbind("click");
			delBtn.on("click", function($event){
				console.log(thisIncLetterSend.TAG + "삭제버튼!");

				thisIncLetterSend.deleteMessage();
			});
			console.log(this.TAG + "End of viewList()");

		},
		saveMessage: function() 
		{
			console.log(this.TAG + "saveMessage()");
			
			var listview = this.HTMLDom.listview;
			var boxList = listview.find("input[type=checkbox]:checked");
			var messageOutSeqList = new Array();
			
			if(boxList.length > 0) {
				for(var i=0; i<boxList.length; i++) 
				{
					var position = boxList.eq(i).attr("id").split("_")[1];
					messageOutSeqList.push(this.letterTotalList[position].messageOutSeq);
				}
		
				var mJson = new MJson();
				mJson.putClient(MJson.CLIENT.USERID, document.globalObj.LOGIN_DATA.userId);
				mJson.putPub(MJson.PUB.SID, "MG001");
				mJson.putPub(MJson.PUB.SERVICENAME, "saveMessage");
				mJson.putParameter(MJson.PARAMETER.MESSAGEINSEQ, messageOutSeqList.join(","));
		
				var cm = new ConnectionManager();
				cm.requestGet(mJson.getJsonString(), function(jsonData){
		
					if(mJson.checkServiceResult(jsonData)) 
					{
						console.log("성공!!!");
						
						thisIncLetterSend.letterTotalList = new Array();
						
						thisIncLetterSend.pageControl.clear();
						
						thisIncLetterSend.getMySentMessageList();
					}
					
				}, function(e){
					alert("error:"+JSON.stringify(e));
				});
			}
			else {
				alert("선택하지 않았거나, 리스트가 없습니다.");
			}
		},
		deleteMessage: function() 
		{
			console.log(this.TAG + "deleteMessage()");
			
			var listview = this.HTMLDom.listview;
			var boxList = listview.find("input[type=checkbox]:checked");
			var messageOutSeqList = new Array();

			if(boxList.length > 0) 
			{
				for(var i=0; i<boxList.length; i++) 
				{
					var position = boxList.eq(i).attr("id").split("_")[1];
					messageOutSeqList.push(this.letterTotalList[position].messageOutSeq);
				}
		
				var mJson = new MJson();
				mJson.putClient(MJson.CLIENT.USERID, document.globalObj.LOGIN_DATA.userId);
				mJson.putPub(MJson.PUB.SID, "MG001");
				mJson.putPub(MJson.PUB.SERVICENAME, "deleteMessage");
				mJson.putParameter(MJson.PARAMETER.MESSAGEOUTSEQ, messageOutSeqList.join(","));
				mJson.putParameter(MJson.PARAMETER.INOUTFLAG, "OUT");

				var cm = new ConnectionManager();
				cm.requestGet(mJson.getJsonString(), function(jsonData){
					
					if(mJson.checkServiceResult(jsonData)) 
					{
						console.log("성공!!!");
						
						thisIncLetterSend.letterTotalList = new Array();
						thisIncLetterSend.pageControl.clear();
						
						thisIncLetterSend.getMySentMessageList();
					}
					
				}, function(e){
					alert("error:"+JSON.stringify(e));
				});
			}
			else {
				alert("선택하지 않았거나, 리스트가 없습니다.");
			}
		}
}